package com.mcb_wxc.service;

import com.mcb_wxc.config.SysConfigure;
import com.mcb_wxc.entity.TAgent;
import com.mcb_wxc.mapper.TAgentMapper;
import com.mcb_wxc.util.CodeUtil;
import com.mcb_wxc.util.PassWordUtil;
import com.mcb_wxc.util.ReturnJsonResultUtil;
import com.mcb_wxc.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by 小生
 * 2019/1/11 17:10.
 * 用户注册
 */
@Api(tags = "USER", description = "用户 ")
@RestController
@RequestMapping(value="/userService")
public class UserService {
    @Autowired
    HttpServletRequest request;
    @Autowired
    TAgentMapper tAgentMapper;
    @Autowired
    SysConfigure sysConfigure;

    /**
     * @api {POST} /userService/addUser 注册用户
     * @apiName addUser
     * @apiGroup user
     * @apiDescription  注册用户
     * @apiVersion 1.0.0
     * 前端调用url：http://192.168.10.181:5258/sellingCarsFrontEnd/sellingCars/login_v2.html
     * @apiParam {String} userName  用户昵称
     * @apiParam {String} password 用户密码
     * @apiSuccess {JsonObject} result 返回对象
     * @apiSuccess {Integer} result.resultCode 调用是否成功, 1 是 0 否.
     * @apiSuccess {String} result.content 返回的描述性话语, 当resultCode为0时输出异常信息
     */
    @ApiOperation(value="addUser", notes="注册用户" , httpMethod = "POST", response = UserService.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户昵称",required = false,dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "password",value = "用户密码",required = false,dataType = "String",paramType="query"),
    })
    @RequestMapping(value = "/addUser" , method = RequestMethod.POST)
    public String addUser(String userName, String password) {
        ReturnJsonResultUtil result = new ReturnJsonResultUtil();
        try{
            if(StringUtil.empty(userName) || StringUtil.empty(password)) {
                result.setContent("参数错误！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            password = PassWordUtil.AESEncode(sysConfigure.getPASSENCODE(),password);
            TAgent tAgent = new TAgent();
            tAgent.setAgentName(userName);
            TAgent tAgente = tAgentMapper.selectOne(tAgent);
            if(tAgente != null) {
                result.setContent("用户名重复！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            tAgent.setPassword(password);
            tAgent.setAgentSex(0);
            tAgent.setAgentAvatar(sysConfigure.getDEFAULTIMG());
            tAgent.setStatus(1);
            tAgent.setCreateTime(new Date());
            tAgentMapper.insert(tAgent);
            result.setContent("注册成功！");
            result.setMainData(tAgent);
        }catch (Exception e) {
            result.printLog(request,e);
            result.setResultCode(CodeUtil.FAIL);
            result.setContent(CodeUtil.SERVER_ERROR);
        }
        return result.toString();
    }

    /**
     * @api {POST} /userService/login 用户登录
     * @apiName login
     * @apiGroup user
     * @apiDescription  用户登录
     * @apiVersion 1.0.0
     * @apiParam {String} userName  用户昵称
     * @apiParam {String} password 用户密码
     * @apiSuccess {JsonObject} result 返回对象
     * @apiSuccess {Integer} result.resultCode 调用是否成功, 1 是 0 否.
     * @apiSuccess {String} result.content 返回的描述性话语, 当resultCode为0时输出异常信息
     */
    @ApiOperation(value="login", notes="用户登录" , httpMethod = "POST", response = UserService.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName",value = "用户昵称",required = false,dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "password",value = "用户密码",required = false,dataType = "String",paramType="query"),
    })
    @RequestMapping(value = "/login" , method = RequestMethod.POST)
    public String login(String userName, String password) {
        ReturnJsonResultUtil result = new ReturnJsonResultUtil();
        try{
            if(StringUtil.empty(userName) || StringUtil.empty(password)) {
                result.setContent("参数错误！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            //对比加密字符密码
            password = PassWordUtil.AESEncode(sysConfigure.getPASSENCODE(),password);
            TAgent tAgent = new TAgent();
            tAgent.setAgentName(userName);
            TAgent tAgente = tAgentMapper.selectOne(tAgent);
            if(tAgente == null) {
                result.setContent("用户还没注册！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            if(!(tAgente.getPassword().equals(password))) {
                result.setContent("密码错误！");
                result.setResultCode(CodeUtil.FAIL);
                return result.toString();
            }
            tAgente.setPassword(null);
            result.setContent("登录成功！");
            result.setMainData(tAgente);
        }catch (Exception e) {
            result.printLog(request,e);
            result.setResultCode(CodeUtil.FAIL);
            result.setContent(CodeUtil.SERVER_ERROR);
        }
        return result.toString();
    }

}
